package sample.util;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.util.Duration;


import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**

 * @name      :Alert

 * @Description: messageBox like stage which will close in specific seconds you set

*/
public class Alert extends Stage {
    AtomicInteger timesRemains;
    Text remain;
    AtomicBoolean TimerClose;
    private void elemInit(){
        this.timesRemains=new AtomicInteger(-1);
        this.remain=new Text("");
        this.remain.setStroke(Color.RED);
        this.TimerClose=new AtomicBoolean(false);
    }
    private void tick(int sec,boolean show){
        this.timesRemains.set(sec);
        try{
            timeFade(show);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public Alert(String msg){
        super();
        this.setTitle("MessageBox");
        Text text=new Text(msg);
        VBox vb=new VBox();
        this.setScene(new Scene(vb,msg.length()*6,100));
        vb.setMinWidth(100);
        this.elemInit();
        vb.setAlignment(Pos.CENTER);
        vb.getChildren().addAll(text,remain);
        this.show();
    }
    public Alert(String msg,int seconds){
        this(msg);
        this.tick(seconds,true);
    }
    public Alert(String Title,int seconds,Scene apart){
        super();
        this.setTitle(Title);
        this.setScene(apart);
        this.elemInit();
        this.tick(seconds,false);
        Alert.TitleTick(this);
    }
    public void setSecondToClose(int seconds){
        this.timesRemains.set(seconds);
    }
    public int getSecToClose(){
        return this.timesRemains.get();
    }
    private void timeFade(boolean show) throws InterruptedException {
        TimerClose.set(false);
        if(timesRemains.get()<0)return;
            // todo some thing wrong with the time display
//        Timer timer=new Timer();
//        TimerTask timerTask=new TimerTask() {
//            @Override
//            public void run() {
//                timeShow();
//                timesRemains.set(timesRemains.get()-1);
//                System.out.println(timesRemains);
//                TimerClose.set(timesRemains.get()<=0);
//                if(TimerClose.get())
//                    timer.cancel();
//            }
//        };
//        timer.scheduleAtFixedRate(timerTask,990,10);
        Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), e->{
            if(show)
                timeShow();
            timesRemains.decrementAndGet();
            if(timesRemains.get()==-1)
                this.close();
        }));
        animation.setCycleCount(timesRemains.get()+1);
        animation.play();
    }
    public int getAndDecrementRemainTime(){
        return this.timesRemains.getAndDecrement();
    }
    public void timeShow(){
        this.remain.setText("It remains "+this.timesRemains+" to close");
        this.remain.setTextAlignment(TextAlignment.CENTER);
    }
    public static void TitleTick(Alert alert){
        final String Title=alert.getTitle();
        Timeline streamline=new Timeline(new KeyFrame(Duration.millis(1000),e->{
            if(alert.getSecToClose()<0){
                alert.setTitle("Long Time Stage");
            }
            else if(alert.getAndDecrementRemainTime()==0){
                alert.close();
            }else{
                alert.setTitle(Title +":"+alert.getSecToClose());
            }
        }));
        streamline.setCycleCount(alert.getSecToClose());
        streamline.play();
    }
}
